<template>
  <div class="login-page">
    <el-card>
      <template #header>黑马面经运营后台</template>
      <!-- 登录表单 -->
      <el-form  :rules="formDataRules" :model="formData" ref="formDataRef" label-position="left">
        <el-form-item label="用户名:" prop="username">
          <el-input placeholder="请输入用户名"  v-model="formData.username"></el-input>
        </el-form-item>
        <el-form-item  label="密码:" prop="password" >
          <el-input placeholder="请输入密码" v-model="formData.password"></el-input>
        </el-form-item >
        <el-form-item>
          <el-button type="primary" @click="doClick">登录</el-button>
          <el-button @click="delClick">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>

import { mapActions } from 'vuex'
export default {
  name: 'login-page',
  data () {
    return {
      formData: {
        username: '',
        password: ''
      },
      formDataRules: {
        username: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { pattern: /^\w{5,15}$/, message: '账号是5-15位', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { pattern: /^\w{5,15}$/, message: '密码是5-15位', trigger: 'blur' }
        ]

      }
    }
  },
  methods: {
    ...mapActions('user', ['getToken']),
    // 检查是否校验成功
    doClick () {
      this.$refs.formDataRef.validate(async valid => {
        // console.log(valid)
        if (valid) {
          // 发送ajax
          // const { data } = await userLogin(this.formData)
          // console.log(data)
          await this.getToken(this.formData)
          // 请求成功跳转首页
          this.$router.push('/')
        }
      })
    },
    delClick () {
      // 点击重置
      this.$refs.formDataRef.resetFields()
      this.formData = {
        username: '',
        password: '',
        repassword: ''
      }
    }
  }
}
</script>
<style lang="scss" scoped>
/* 1.deep深度选择器作用: 让父组件 可以选中 子组件文件内部的盒子
 2.deep语法:
  scss语言: ::v-deep
  less语言: /deep/
  3. deep场景: 需要在组件中选中子组件内部的盒子 */
.login-page {
  min-height: 100vh;
  background: url(@/assets/login-bg.svg) no-repeat center / cover;
  display: flex;
  align-items: center;
  justify-content: space-around;
  .el-card {
    width: 420px;
    /* 深度选择器 */
    ::v-deep .el-card__header {
      height: 80px;
      background: rgba(114, 124, 245, 1);
      text-align: center;
      line-height: 40px;
      color: #fff;
      font-size: 18px;
    }
  }
  .el-form {
    padding: 0 20px;
  }
  .tc {
    text-align: center;
  }

}
</style>
